<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\base\Object;
use app\models\Admin;
use app\models\YkUser;

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin()
    {       
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {

            return $this->goBack();
        } 
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
    
    public function actionCache()
    {
        
       /*  $key = md5('information');
        $result = YkUser::findBySql('select id,user_name from yk_user limit 0,3000')->asArray()->all();
       
        yii::$app->cache->set($key,$result);   */
         
        $items = array(
            '1'=>'lize','2'=>'zhangqian'
        );
        yii::$app->cache->mset($items);
        
        //redis hash操作
        yii::$app->redis->executeCommand('HMSET', ['user:1', 'name', 'joe', 'solary', 2000]);
    }
    
    public function actionList()
    {
        $first_memory = memory_get_usage();
        echo "第一次内存:".$first_memory."<br />";
       $result = YkUser::findBySql('select id,user_name from yk_user limit 0,3000')->asArray()->all();
        
       $send_memory  = memory_get_usage();
       echo "第二次内存:".$send_memory."<br />";
       
       echo "数据所占内存为:".(($send_memory-$first_memory)/1000000)."M";
       exit; 
    }
    
    public function actionTest()
    { 
        //redis hash操作
        var_dump(yii::$app->redis->executeCommand('hVals',['user:1']));
       
        $test = array(1,2);
       var_dump(yii::$app->cache->mget($test));
       
       var_dump(yii::$app->cache->get(1));
        /*   $key = md5('information');
       
        if(($k = yii::$app->cache->get($key))){ //判断是否获取到指定的key
            print_r($k);exit;
        } else {
            echo 'normal'; //这里我们在实际使用中就需要替换成查询数据库并创建缓存.
        }   */
    }
    
    public function actionFlush()
    {

        yii::$app->cache->flush();
    }
}
